/*
 * ValuedModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.request.OrderRequest;
import com.yidiandao.business.data.request.UploadLocalOfferImageRequest;
import com.yidiandao.business.data.request.ValuedCommendRequest;
import com.yidiandao.business.data.request.ValuedRequest;
import com.yidiandao.business.data.response.UploadLicenseResponse;
import com.yidiandao.business.data.response.ValuedResponse;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.RateGoodsInfoListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/** 评论 */
public class ValuedModel implements IValuedModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void getOrderInfo(String tag, String orderId, final onAccessNetListener listener) {
        OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderId);
        request.setUrl(Urls.COMMENT_ORDER);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, ValuedResponse.class, new NetListener<ValuedResponse>() {
            @Override
            public void onSuccess(ValuedResponse valuedResponse) {
                ValuedResponse.DataBean data = valuedResponse.getData();
                ArrayList<DisplayListItem> displayListItems = new ArrayList<>();
                List<Goods> goodsList = data.getGoodsList();
                if (null != goodsList && goodsList.size() != 0) {
                    for (int i = 0; i < goodsList.size(); i++) {
                        displayListItems.add(new RateGoodsInfoListItem(goodsList.get(i)));
                    }
                }

                if (listener != null) {
                    listener.onSuccess(displayListItems);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void commentOrder(
            String tag,
            String orderId,
            float comment_rank,
            float shipping_rank,
            float serve_rank,
            final onCommendListener listener) {
        ValuedCommendRequest request = new ValuedCommendRequest();
        request.setTag(tag);
        request.setOrderId(orderId);
        request.setUrl(Urls.COMMENT_ORDER);
        request.setComment_rank(comment_rank);
        request.setShipping(shipping_rank);
        request.setServer(serve_rank);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void sendPic(String tag, File file, String time, final onCommendListener listener) {
        UploadLocalOfferImageRequest request = new UploadLocalOfferImageRequest();
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.addUploadFile("picture", file);
        request.setUrl(Urls.UPLOAD_PIC);
        request.setShaidanImg(time);
        mNetBiz.uploadFiles(request, UploadLicenseResponse.class,
                new NetListener<UploadLicenseResponse>() {
                    @Override
                    public void onSuccess(UploadLicenseResponse response) {
                        if (listener != null) {
                            listener.onSuccess();
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void sendText(
            String tag,
            String recId,
            String order_id,
            String message,
            Float rank,
            String time,
            final onCommendListener listener) {
        ValuedRequest request = new ValuedRequest();
        request.setTag(tag);
        request.setUrl(Urls.COMMENT_GOODS);
        request.setRecId(recId);
        request.setGoodsRank(rank);
        request.setMessage(message);
        request.setShaidanImg(time);
        request.setGoodsId(order_id);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }
}
